/*
 * This file is Copyright 2002-2006 Loyola University Chicago,
 * Department of Computer Science and the Emerging Technologies Laboratory.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License via
 *    http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.citep.web.gwt.widgets;

import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ChangeListener;

public class CalendarEntry extends HorizontalPanel {
	protected TextBox textField;
	protected Image calendarIcon;
	
	
	protected void init() {

		final PopupCalendar calendar = PopupCalendar.getInstance();

		//when the user clicks on the calendar icon, the calendar should
		//be displayed
		calendarIcon.addClickListener(new ClickListener() {
			public void onClick(Widget w) {
				
				int calendarOffsetWidth = 250; //idaelly use calendar.getOffsetWidth(), but since it's not on screen it returns 0
				
				int yCoord = calendarIcon.getAbsoluteTop() + calendarIcon.getOffsetHeight();
				int right = calendarIcon.getAbsoluteLeft() + calendarOffsetWidth;
				
				int xCoord;
				if(Window.getClientWidth() < right)
					xCoord = calendarIcon.getAbsoluteLeft() + calendarIcon.getOffsetWidth() - calendarOffsetWidth;
				else
					xCoord = calendarIcon.getAbsoluteLeft();
				
				calendar.setPopupPosition(xCoord, yCoord);


				//remove previos change listeners. This is in case we
				//have several CalendarEntry widgets on the same view
				calendar.getCalendar().clearChangeListeners();
				
				//wire up the calendar & text field so that whenever the user
				//selects a date, the textfield reflects that date
				//after setting the date, hide the calendar
				calendar.getCalendar().addChangeListener(new ChangeListener() {
					public void onChange(Widget w) {
						String date = "" +	(calendar.getCalendar().getMonth()+1) + "/" +
											calendar.getCalendar().getDay() + "/" +
											calendar.getCalendar().getYear();
						
						textField.setText(date);
						calendar.hide();
					}
				});

				
				calendar.show();

				//now that the calnedar is showing, set the correct position
				if(Window.getClientWidth() < right) {
						xCoord = calendarIcon.getAbsoluteLeft() + calendarIcon.getOffsetWidth() - calendar.getOffsetWidth();
						calendar.setPopupPosition(xCoord, yCoord);
				}
			}
		});
	}
	
	public CalendarEntry(String iconUrl) {
		calendarIcon = new Image(iconUrl);
		
		textField = new TextBox();
		textField.setMaxLength(10);
		textField.setVisibleLength(10);
		
		add(textField);
		add(calendarIcon);
		setSpacing(0);
		
		init();
	}
	
	public TextBox getTextBox() {
		return textField;
	}
}
